package ar.com.pragmatia.lanacion;

import android.app.Activity;
import android.app.ProgressDialog;
import android.content.Intent;
import android.location.Location;
import android.os.AsyncTask;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import ar.com.pragmatia.lanacion.data.Syncronizer;

public class LaNacionActivity extends Activity {

	private Button btnClients;
	private Button btnRoadMaps;
	private Button btnOrders;
	private Button btnVisits;
	private Button btnUpdate;

	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		// setContentView(R.layout.main_screen);

	}

	public void onStart() {
		super.onStart();

		btnClients = (Button) findViewById(R.id.btnClients);
		btnRoadMaps = (Button) findViewById(R.id.btnRoadMaps);
		btnOrders = (Button) findViewById(R.id.btnOrders);
		btnVisits = (Button) findViewById(R.id.btnVisits);
		btnUpdate = (Button) findViewById(R.id.btnUpdate);

		OnClickListener clientsListener = new OnClickListener() {

			public void onClick(View v) {
				Intent clientIntent = new Intent(getApplicationContext(),
						MainClientActivity.class);
				startActivity(clientIntent);
			}

		};
		btnClients.setOnClickListener(clientsListener);

		OnClickListener roadMapListener = new OnClickListener() {

			public void onClick(View v) {
				Intent roadMapIntent = new Intent(getApplicationContext(),
						MainRoadMapActivity.class);
				startActivity(roadMapIntent);
			}

		};
		btnRoadMaps.setOnClickListener(roadMapListener);

		OnClickListener ordersListener = new OnClickListener() {

			public void onClick(View v) {
				Intent ordersIntent = new Intent(getApplicationContext(),
						MainOrderActivity.class);
				startActivity(ordersIntent);
			}

		};
		btnOrders.setOnClickListener(ordersListener);

		OnClickListener visitsListener = new OnClickListener() {

			public void onClick(View v) {
				Intent visitsIntent = new Intent(getApplicationContext(),
						MainVisitActivity.class);
				startActivity(visitsIntent);
			}

		};
		btnVisits.setOnClickListener(visitsListener);

		OnClickListener updateListener = new OnClickListener() {

			public void onClick(View v) {
				new RetreiveFeedTask().execute();
			}

		};
		btnUpdate.setOnClickListener(updateListener);
	}

	class RetreiveFeedTask extends AsyncTask<Void, Void, Void> {

		private ProgressDialog dialog = null;

		@Override
		protected void onPreExecute() {
			// TODO Auto-generated method stub
			super.onPreExecute();
			dialog = ProgressDialog.show(LaNacionActivity.this,
					getString(R.string.wait), getString(R.string.loading),
					true, false);
		}

		@Override
		protected void onPostExecute(Void result) {
			super.onPostExecute(result);

			dialog.dismiss();
		}

		@Override
		protected Void doInBackground(Void... arg0) {
			Syncronizer.sync();
			return null;
		}
	}

	private static final int TWO_MINUTES = 1000 * 30;

	/**
	 * Determines whether one Location reading is better than the current
	 * Location fix
	 * 
	 * @param location
	 *            The new Location that you want to evaluate
	 * @param currentBestLocation
	 *            The current Location fix, to which you want to compare the new
	 *            one
	 */
	public static boolean isBetterLocation(Location location,
			Location currentBestLocation) {
		if (currentBestLocation == null) {
			// A new location is always better than no location
			return true;
		}

		// Check whether the new location fix is newer or older
		long timeDelta = location.getTime() - currentBestLocation.getTime();
		boolean isSignificantlyNewer = timeDelta > TWO_MINUTES;
		boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES;
		boolean isNewer = timeDelta > 0;

		// If it's been more than two minutes since the current location, use
		// the new location
		// because the user has likely moved
		if (isSignificantlyNewer) {
			return true;
			// If the new location is more than two minutes older, it must be
			// worse
		} else if (isSignificantlyOlder) {
			return false;
		}

		// Check whether the new location fix is more or less accurate
		int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation
				.getAccuracy());
		boolean isLessAccurate = accuracyDelta > 0;
		boolean isMoreAccurate = accuracyDelta < 0;
		boolean isSignificantlyLessAccurate = accuracyDelta > 200;

		// Check if the old and new location are from the same provider
		boolean isFromSameProvider = isSameProvider(location.getProvider(),
				currentBestLocation.getProvider());

		// Determine location quality using a combination of timeliness and
		// accuracy
		if (isMoreAccurate) {
			return true;
		} else if (isNewer && !isLessAccurate) {
			return true;
		} else if (isNewer && !isSignificantlyLessAccurate
				&& isFromSameProvider) {
			return true;
		}
		return false;
	}

	/** Checks whether two providers are the same */
	private static boolean isSameProvider(String provider1, String provider2) {
		if (provider1 == null) {
			return provider2 == null;
		}
		return provider1.equals(provider2);
	}
}
